package com.zrx.govern.component.easyexcel.starter.controller;

import com.zrx.govern.component.easyexcel.starter.entity.ExcelCheckResult;
import com.zrx.govern.component.easyexcel.starter.entity.GovernResult;
import com.zrx.govern.component.easyexcel.starter.service.impl.EasyExcelOperationService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

import static com.zrx.govern.component.easyexcel.starter.enums.ExcelEnum.XLSX;

/**
 * 通用Excel相关接口
 *
 * @author zhangxiaowei
 */
@RestController
@RequestMapping("/common/excel")
@Slf4j
public class CommonExcelController {

    @Autowired
    private EasyExcelOperationService easyExcelOperationService;

    @ApiOperation(value = "上传文件")
    @RequestMapping(value = "/import", method = RequestMethod.POST)
    public GovernResult<ExcelCheckResult> importExcel(@RequestParam String model, @RequestParam(value = "file") MultipartFile file) {
        ExcelCheckResult excelCheckResult = easyExcelOperationService.excelRead(model, file);
        return GovernResult.ok(excelCheckResult);
    }

    @ApiOperation(value = "保存入库")
    @RequestMapping(value = "/storage", method = RequestMethod.GET)
    public GovernResult<Integer> storage(@RequestParam String model) {
        Integer count = easyExcelOperationService.storage(model);
        return GovernResult.ok(count);
    }

    @ApiOperation(value = "下载校验文件")
    @RequestMapping(value = "/checkExcel", method = RequestMethod.GET)
    public void exportExcel(@RequestParam String model, HttpServletResponse response) {
        easyExcelOperationService.downloadCheckExcel(model,response);
    }

    @ApiOperation(value = "取消excel上传")
    @RequestMapping(value = "/cancelUpload", method = RequestMethod.GET)
    public GovernResult deleteImportFile(@RequestParam String model) {
        easyExcelOperationService.cancelExcelImport(model);
        return GovernResult.ok("删除成功");
    }

    @ApiOperation(value = "取消导入操作")
    @RequestMapping(value = "/cancel", method = RequestMethod.GET)
    public GovernResult cancel(@RequestParam String model) {
        easyExcelOperationService.cancel(model);
        return GovernResult.ok();
    }

    @ApiOperation(value = "模板下载")
    @RequestMapping(value = "/template", method = RequestMethod.GET)
    public void downloadTemplate(@RequestParam String model, HttpServletResponse response) {
        easyExcelOperationService.downloadTemplate(model, response, XLSX);
    }

    /**
     *
     * @param model
     * @param type  1:导入 2:导出 3:校验
     * @return
     */
    @ApiOperation(value = "获取文件进度")
    @RequestMapping(value = "/progress", method = RequestMethod.GET)
    public GovernResult<String> getProgress(@RequestParam String model,@RequestParam String type) {
        String progress = easyExcelOperationService.getProgress(model, type);
        return GovernResult.ok(progress, "");
    }

}
